package org.icress.fusion.snapshot

import org.icress.fusion.admin.ShareRoleService
import org.springframework.transaction.annotation.*

import org.icress.fusion.shiro.User
import org.apache.shiro.SecurityUtils

class CreditControlService {

    static transactional = true

    def shareRoleService
    def grailsApplication

    //获取出站消息列表
    
    @Transactional(readOnly = true)
    def list(Object params) {
        //get current user
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        
        def shareUsers = shareRoleService.getShareUserList("creditControl", "list")

        def creditControlInstanceList = CreditControl.withCriteria{
            //返回条数
            if(params?.max) maxResults(params.int('max'))
            //偏移量
			if(params?.offset) firstResult(params.int('offset'))
            //排序
			if(params?.sort && params?.order) order(params?.sort, params?.order)
            //stage
            //if(params?.stage) eq("stage", params?.stage)
            if(currentUser.username != 'admin') {
    	        or{
                    inList("owner.id", shareUsers)
    			    eq("owner", currentUser)
    		    }
            }
        }
        return creditControlInstanceList
    }

    @Transactional(readOnly = true)
    def count() {
        //get current user
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        def shareUsers = shareRoleService.getShareUserList("creditControl", "list")
        def count = CreditControl.withCriteria{
			projections{
				rowCount()
			}
            if(currentUser.username != 'admin') {
    	        or{
                    inList("owner.id", shareUsers)
    			    eq("owner", currentUser)
    		    }
            }
        }
        return (count ==null)? 100:  count[0]
    }
    
    @Transactional(readOnly = true)
    def show(id) {
        //get current user
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        
        def shareUsers = shareRoleService.getShareUserList("creditControl", "show")

        def creditControlInstance = CreditControl.withCriteria(uniqueResult:true){
            eq("id", id.toLong())
            if(currentUser.username != 'admin') {
    	        or{
                    inList("owner.id", shareUsers)
    			    eq("owner", currentUser)
    		    }
            }
        }
        return creditControlInstance
    }

    def edit(id) {
        //get current user
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        
        def shareUsers = shareRoleService.getShareUserList("creditControl", "edit")

        def creditControlInstance = CreditControl.withCriteria(uniqueResult:true){
            eq("id", id.toLong())
            if(currentUser.username != 'admin') {
    	        or{
                    inList("owner.id", shareUsers)
    			    eq("owner", currentUser)
    		    }
            }
        }
        return creditControlInstance
    }

    def delete(id) {
        //get current user
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        
        def shareUsers = shareRoleService.getShareUserList("creditControl", "delete")

        def creditControlInstance = CreditControl.withCriteria(uniqueResult:true){
            eq("id", id.toLong())
            if(currentUser.username != 'admin') {
    	        or{
                    inList("owner.id", shareUsers)
    			    eq("owner", currentUser)
    		    }
            }
        }
        return creditControlInstance
    }
}
